package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 插入订单数据
     * @param order
     */
    void insert(Orders order);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 条件检索
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> selectOrderByCondition(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 待派送数量查询
     * @return
     */
    @Select("select count(*) from orders where status = 3")
    Integer selectConfirmedOrder();

    /**
     * 派送中数量查询
     * @return
     */
    @Select("select count(*) from orders where status = 4")
    Integer selectDeliveryInProgressOrder();

    /**
     * 待接单数量查询
     * @return
     */
    @Select("select count(*) from orders where status = 2")
    Integer selectToBeConfirmedOrder();

    /**
     * 用户订单查询
     * @param userId
     * @return
     */
    @Select("select * from orders where user_id = #{userId}")
    Page<Orders> selectOrderByUserId(Long userId);


    /**
     * 查询超时订单
     * @return
     */
    @Select("select * from orders where status = 1 and order_time < #{time}")
    List<Orders> selectTimeOutOrder(LocalDateTime time);

    /**
     * 批量删除
     * @param ordersList
     */
    void deleteBatch(List<Orders> ordersList);

    /**
     * 批量更新订单
     * @param ordersList
     */
    void updateBatch(List<Orders> ordersList);

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders selecOrderById(Long id);

    /**
     * 统计时间段内的总额
     * @param begin
     * @param end
     * @return
     */
    @Select("select sum(amount) from orders where order_time >= #{begin} and order_time <= #{end} and status = 5")
    Double countOrderAmountByTime(@Param("begin") LocalDateTime begin ,@Param("end") LocalDateTime end);

    /**
     * 统计时间段内的订单用户数量
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(user_id) from orders where order_time >= #{begin} and order_time <= #{end} and status = 5")
    Integer countOrderUserByTime(@Param("begin") LocalDateTime begin ,@Param("end") LocalDateTime end);

    Integer countByMap(Map map);
}
